package com.dyh.quickdemozwei;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;

import java.util.*;

@Slf4j
public class TimeLog {

    static class LogTimer{


        static class Cell{
            public Cell(String id, long start) {
                this.id = id;
                this.start = start;
            }

            String id;
            long start;
            long delta;
        }

        private   final Map<String, Long> data = new HashMap<>();
        private   final Map<String, Map<String,Cell>> data2 = new HashMap<>();
        private  final  Logger logger;

        public LogTimer(Logger logger) {
            this.logger = logger;
        }


        public void start(String topic,String id){
            data2.computeIfAbsent(topic,k->new LinkedHashMap<>());

            logger.info("{}{}开始执行",topic,id);
        }

        public void end(String topic,String id){
            long last =  data.remove(topic);
            logger.info("{} 结束执行 耗时{} 毫秒",topic, System.currentTimeMillis()-last);

        }

        public void statistics(String topic){

        }

        public void start(String topic){
            data.put(topic,System.currentTimeMillis());
            logger.info("{} 开始执行",topic);
        }

        public void end(String topic){
            long last =  data.remove(topic);
            logger.info("{} 结束执行 耗时{} 毫秒",topic, System.currentTimeMillis()-last);

        }

    }

    @Test
    public void test() throws InterruptedException {
        LogTimer logTimer = new LogTimer(log);

        logTimer.start("a");
        Thread.currentThread().sleep(1000L);
        logTimer.end("a");

        logTimer.start("b");
        Thread.currentThread().sleep(1500L);
        logTimer.end("b");
    }

    @Test public void test2(){
        String s= "/jst";
        String ss= s.substring(1);

        System.out.println();
    }

    @Test public void test3(){
        String s= "catAttrName批准文号";
        String ss= s.substring(11);

        System.out.println();
    }

    @Test public void test4(){
        Lists.partition(Lists.newArrayList(1,2,3,4,5),
        2).stream().forEach(e-> System.out.println(JSON.toJSONString(e)));
    }


}
